package com.jl.core.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jl.config.PageModel;
import com.jl.constants.UserRole;
import com.jl.core.mapper.SysExchangeMapper;
import com.jl.core.mapper.SysOperateRecordMapper;
import com.jl.core.mapper.SysProxyMapper;
import com.jl.core.mapper.UserMapper;
import com.jl.core.model.SysExchange;
import com.jl.core.model.SysOperateRecord;
import com.jl.core.model.SysProxy;
import com.jl.core.model.enums.ProxyType;
import com.jl.core.model.resp.*;
import com.jl.core.service.UserProxyService;
import com.jl.enums.OperateType;
import com.jl.mapper.PercentageMapper;
import com.jl.model.User;
import com.jl.model.resp.ExchangeRecordResp;
import com.jl.model.resp.ExchangeResp;
import com.jl.model.resp.RebateDetailResp;
import com.jl.model.resp.RebateInfoResp;
import com.jl.model.vo.UserVo;
import com.jl.service.GetUserInfoService;
import com.jl.utils.UserInfoContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
@Transactional
public class UserProxyServiceImpl implements UserProxyService {

//    private String QRCode = "http://localhost:9001/proxy/download";

    private static final String QRCode = "http://8.129.181.56:9001/photo/QRCode.jpg";

    @Resource
    private UserMapper userMapper;
    @Resource
    private SysProxyMapper sysProxyMapper;
    @Resource
    private GetUserInfoService getUserInfoService;
    @Resource
    private PercentageMapper percentageMapper;
    @Resource
    private SysExchangeMapper sysExchangeMapper;
    @Resource
    private SysOperateRecordMapper sysOperateRecordMapper;

    @Override
    public PopularizationResp popularization() {
        UserVo user = UserInfoContext.getUser();
        PopularizationResp popularizationResp = sysProxyMapper.selectMyPopularization(user.getId());
        popularizationResp.setQRCode(QRCode);
        return popularizationResp;
    }

    @Override
    public void addMyPlayer(Integer userId) {
        UserVo user = UserInfoContext.getUser();
        SysProxy sysProxy = new SysProxy();
        sysProxy.setCreateTime(new Date());
        sysProxy.setProxyUser(user.getId());
        sysProxy.setType(ProxyType.type1.getId());
        sysProxy.setUserId(userId);
        sysProxyMapper.insertUseGeneratedKeys(sysProxy);
    }

    @Override
    public ProxyPlayerResp player(Integer userId,Integer pageNumb, Integer pageSize) {
        UserVo user = UserInfoContext.getUser();
        User user1 = userMapper.selectByPrimaryKey(user.getId());
        PageHelper.startPage(pageNumb,pageSize);
        ProxyPlayerResp proxyPlayerResp = new ProxyPlayerResp();
        List<MyPlayer> list =  sysProxyMapper.selectMyPlayer(userId,user.getId());
        if (user1.getProxyRate()!=null){
            for (MyPlayer myPlayer : list) {
                myPlayer.setProxyRate(getProxyRate(user1.getProxyRate(),"0.05"));
            }
        }
        PageInfo<MyPlayer> playerPageInfo = new PageInfo<>(list);
        PageModel<MyPlayer> playerPageModel = new PageModel<>(playerPageInfo);
        proxyPlayerResp.setPlayers(playerPageModel);
        Integer count = sysProxyMapper.selectPlayerAdd(user.getId());
        proxyPlayerResp.setAddToday(count);
        return proxyPlayerResp;
    }

    private List<String> getProxyRate(BigDecimal bigDecimal,String min){
        List<String> list = new ArrayList<>();
        while (true){
            bigDecimal = bigDecimal.subtract(new BigDecimal("0.05"));
            list.add(bigDecimal.toString());
            if (bigDecimal.compareTo(new BigDecimal(min))==0){
                break;
            }
        }
        return list;
    }


    @Override
    public void bindInvCode(Integer code) {
        UserVo user = UserInfoContext.getUser();
        getUserInfoService.changeAccount(1,user.getId(),new BigDecimal(6));
        SysProxy sysProxy = new SysProxy();
        sysProxy.setUserId(code);
        sysProxy.setProxyUser(user.getId());
        sysProxy.setCreateTime(new Date());
        sysProxy.setType(ProxyType.type1.getId());
        sysProxyMapper.insertUseGeneratedKeys(sysProxy);
    }

    @Override
    public String invCode() {
        UserVo user = UserInfoContext.getUser();
        return sysProxyMapper.selectInvCode(user.getId());
    }

    @Override
    public void upgrade(Integer userId,String proxyRate) {
        User user = new User();
        user.setId(userId);
        user.setProxyRate(new BigDecimal(proxyRate));
        user.setUserRole(UserRole.代理.getValue());
        userMapper.updateByPrimaryKeySelective(user);
        sysProxyMapper.updatePlayerToProxy(userId);
    }

    @Override
    public ProxyResp myProxy(Integer userId, Integer pageNumb, Integer pageSize) {
        UserVo user = UserInfoContext.getUser();
        User user1 = userMapper.selectByPrimaryKey(user.getId());
        PageHelper.startPage(pageNumb,pageSize);
        List<MyProxy> list = sysProxyMapper.selectMyProxy(userId,user.getId());
        if (user1.getProxyRate()!=null){
            for (MyProxy myProxy : list) {
                myProxy.setProxyRate(getProxyRate(user1.getProxyRate(),myProxy.getMyProxyRate().toString()));
            }
        }
        PageInfo<MyProxy> playerPageInfo = new PageInfo<>(list);
        PageModel<MyProxy> playerPageModel = new PageModel<>(playerPageInfo);
        Integer count = sysProxyMapper.selectProxyAdd(user.getId());
        ProxyResp proxyResp = new ProxyResp();
        proxyResp.setAddToday(count);
        proxyResp.setProxies(playerPageModel);
        return proxyResp;
    }

    @Override
    public void updateRate(Integer userId, String proxyRate) {
        sysProxyMapper.updateProxyRate(userId,proxyRate);
    }

    @Override
    public RebateInfoResp rebate(Long startDate, Long endDate) {
        UserVo user = UserInfoContext.getUser();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("MM-dd");
        String start = simpleDateFormat.format(new Date(startDate));
        String end = simpleDateFormat.format(new Date(endDate));
        String startResp = simpleDateFormat2.format(new Date(startDate));
        String endResp = simpleDateFormat2.format(new Date(endDate));
        RebateInfoResp rebateInfoResp = percentageMapper.selectRebateInfo(user.getId(),start,end);
        if (rebateInfoResp!=null){
            rebateInfoResp.setDate(startResp+"-"+endResp);
        }
        return rebateInfoResp;
    }

    @Override
    public PageModel<RebateDetailResp> rebateDetail(Long startDate, Long endDate,String type,Integer pageNumb, Integer pageSize) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        UserVo user = UserInfoContext.getUser();
        String start = simpleDateFormat.format(new Date(startDate));
        String end = simpleDateFormat.format(new Date(endDate));
        PageHelper.startPage(pageNumb,pageSize);
        List<RebateDetailResp> rebateDetailResp = percentageMapper.selectRebateDetail(user.getId(),start,end,type);
        PageInfo<RebateDetailResp> playerPageInfo = new PageInfo<>(rebateDetailResp);
        PageModel<RebateDetailResp> playerPageModel = new PageModel<>(playerPageInfo);
        return playerPageModel;
    }

    @Override
    public ExchangeResp queryExchange(Integer pageNumb, Integer pageSize) {
        UserVo user = UserInfoContext.getUser();
        ExchangeResp exchangeResp = percentageMapper.selectExchange(user.getId());
        //查询已兑换的返利
        String coin = percentageMapper.selectFinishExchange(user.getId());
        if (StringUtils.isEmpty(coin)){
            coin = "0";
        }
        //查询已兑换记录
        PageHelper.startPage(pageNumb,pageSize);
        List<ExchangeRecordResp> list = sysExchangeMapper.selectExchangeRecord(user.getId());
        PageInfo<ExchangeRecordResp> playerPageInfo = new PageInfo<>(list);
        PageModel<ExchangeRecordResp> playerPageModel = new PageModel<>(playerPageInfo);
        if (exchangeResp!=null){
            exchangeResp.setAbleRebate(exchangeResp.getAbleRebate().subtract(new BigDecimal(coin)));
            exchangeResp.setRecord(playerPageModel);
        }
        return exchangeResp;
    }

    @Override
    public void exchangeCoin(String coin,String rest) {
        UserVo user = UserInfoContext.getUser();
        SysExchange sysExchange = new SysExchange();
        sysExchange.setCreateTime(new Date());
        sysExchange.setExchangeCoin(new BigDecimal(coin));
        sysExchange.setType("1");
        sysExchange.setStatus("1");
        sysExchange.setUserId(user.getId());
        getUserInfoService.changeAccount(1,user.getId(),new BigDecimal(coin));
        sysExchangeMapper.insertUseGeneratedKeys(sysExchange);
        //插入记录表
        SysOperateRecord sysOperateRecord = new SysOperateRecord();
        sysOperateRecord.setCreateTime(new Date());
        sysOperateRecord.setUserId(user.getId());
        sysOperateRecord.setMoney(new BigDecimal(coin));
        sysOperateRecord.setRemainderMoney(new BigDecimal(rest));
        sysOperateRecord.setType(OperateType.type2.getId());
        sysOperateRecordMapper.insertUseGeneratedKeys(sysOperateRecord);
    }

}
